from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from goods.models import GoodsChannel

from goods.models import GoodsChannelGroup

from goods.models import GoodsCategory
from meiduo_admin.serializers.channels import GoodsChannelSerializer, ChannelSerializer
from meiduo_admin.serializers.skus import GoodsCategorySerializer
from meiduo_mall.apps.meiduo_admin.utils import PageNum


class ChannelsView(ModelViewSet):
    # 指定查询集
    queryset = GoodsChannel.objects.all()
    # 指定序列化器
    serializer_class = GoodsChannelSerializer
    # 指定分页器
    pagination_class = PageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    @action(methods=['get'], detail=False)
    def categories(self,request):
        data = GoodsCategory.objects.filter(parent=None)
        ser = GoodsCategorySerializer(data,many=True)
        return Response(ser.data)
    def channel_types(self,request):
        Group = GoodsChannelGroup.objects.all()
        # 序列化返回ｓｋｕ商品
        ser = ChannelSerializer(Group,many=True)
        return Response(ser.data)
